package com.big.book_jobs;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.big.book_jobs.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {

	private static final String TEMP = "temp";
	ArrayList<String> listName = new ArrayList<String>();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		SharedPreferences sp = getSharedPreferences(TEMP, MODE_WORLD_READABLE);
	    int chapter = sp.getInt("chapter", -1);
	    Log.i("store", chapter + "");
		if(chapter != -1){
//		showBackConfirm("是否返回上次阅读到的地方");   
		}		
		init();
		ListView lv = (ListView) findViewById(R.id.gridView1);
		
		ArrayAdapter<String> adpter = new ArrayAdapter<String>(this, R.layout.item, R.id.online_user_list_item_textview,listName);
 		lv.setAdapter(adpter);
 		
 		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Bundle bundle = new Bundle();
				bundle.putInt("chapter", position);
				bundle.putInt("page", 0);
				bundle.putInt("comeStatus", 2);
				bundle.putInt("maxChapter", listName.size());
				Intent intent = new Intent();
				intent.putExtras(bundle);
				intent.setClass(MainActivity.this, PageActivity.class);
				startActivity(intent);
			}
		});
	}
	
	/**
	 *  初始化页面的数据
	 */
	void init(){
		String book = getTotalBook();
		Pattern p = Pattern.compile("<t>.*</t>");
		Matcher matcher = p.matcher(book);
		while(matcher.find()){
			String tmp = matcher.group();
			tmp = tmp.replace("<t>", "");
			tmp = tmp.replace("</t>", "");
			listName.add(tmp);
		}

		Pattern p2 = Pattern.compile("<h>.*</h>");
		Matcher matcher2 = p2.matcher(book);
		TextView title = (TextView) findViewById(R.id.title);
		if(matcher2.find()){
			String tmp = matcher2.group();
			tmp = tmp.replace("<h>", "");
			tmp = tmp.replace("</h>", "");
			title.setText(tmp);
		}

		Pattern p3 = Pattern.compile("<a>.*</a>");
		Matcher matcher3 = p3.matcher(book);
		TextView author = (TextView) findViewById(R.id.author);
		if(matcher3.find()){
			String tmp = matcher3.group();
			tmp = tmp.replace("<a>", "");
			tmp = tmp.replace("</a>", "");
			author.setText(tmp);
		}
	}
	
	
	/**
	 * @return  返回截取的字符串
	 */
	public  String getTotalBook(){
		InputStream is=null;
		try {
			is = getAssets().open("book/" + "chapter@_0.txt");
			byte[] b = new byte[is.available()];
			is.read(b);
			return new String(b,"UTF-8");
					
		} catch (IOException e) {                   
			e.printStackTrace();
		}  	
		finally
			//关闭输入流  @liuliang
		{
			if(is != null){
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}				
						}					
			}			
		
		return null; 
	}
	
    public void showBackConfirm(final String message) {
        runOnUiThread(new Runnable() {
            public void run() {
                AlertDialog.Builder builder = new Builder(MainActivity.this);
                builder.setMessage(message);
                builder.setNegativeButton("否", new DialogInterface.OnClickListener(){

					@Override
					public void onClick(DialogInterface dialog, int which) {
						return;
					}})
                .setPositiveButton("是", new DialogInterface.OnClickListener(){
					@Override
					public void onClick(DialogInterface dialog, int which) {
					    SharedPreferences sp = getSharedPreferences(TEMP, MODE_WORLD_READABLE);
					    Bundle bundle = new Bundle();
					    int chapter = sp.getInt("chapter", -1);
					    int page = sp.getInt("page", -1);
						bundle.putInt("chapter",chapter);
						bundle.putInt("page", page);
						Log.i("store", chapter + "intent");
					    Intent intent = new Intent();
					    intent.putExtras(bundle);
					    intent.setClass(MainActivity.this, PageActivity.class);
					    startActivity(intent);
					}}).show();
            }
        });
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getRepeatCount() == 0) {
        	android.os.Process.killProcess(android.os.Process.myPid()); 
        	System.exit(0);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    
}
